{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Find the Value of the Partition"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findValueOfPartition"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #找出分区值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个 <strong>正</strong> 整数数组 <code>nums</code> 。</p>\n",
    "\n",
    "<p>将 <code>nums</code> 分成两个数组：<code>nums1</code> 和 <code>nums2</code> ，并满足下述条件：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>数组 <code>nums</code> 中的每个元素都属于数组 <code>nums1</code> 或数组 <code>nums2</code> 。</li>\n",
    "\t<li>两个数组都 <strong>非空</strong> 。</li>\n",
    "\t<li>分区值 <strong>最小</strong> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>分区值的计算方法是 <code>|max(nums1) - min(nums2)|</code> 。</p>\n",
    "\n",
    "<p>其中，<code>max(nums1)</code> 表示数组 <code>nums1</code> 中的最大元素，<code>min(nums2)</code> 表示数组 <code>nums2</code> 中的最小元素。</p>\n",
    "\n",
    "<p>返回表示分区值的整数。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [1,3,2,4]\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>可以将数组 nums 分成 nums1 = [1,2] 和 nums2 = [3,4] 。\n",
    "- 数组 nums1 的最大值等于 2 。\n",
    "- 数组 nums2 的最小值等于 3 。\n",
    "分区值等于 |2 - 3| = 1 。\n",
    "可以证明 1 是所有分区方案的最小值。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [100,1,10]\n",
    "<strong>输出：</strong>9\n",
    "<strong>解释：</strong>可以将数组 nums 分成 nums1 = [10] 和 nums2 = [100,1] 。 \n",
    "- 数组 nums1 的最大值等于 10 。 \n",
    "- 数组 nums2 的最小值等于 1 。 \n",
    "分区值等于 |10 - 1| = 9 。 \n",
    "可以证明 9 是所有分区方案的最小值。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [find-the-value-of-the-partition](https://leetcode.cn/problems/find-the-value-of-the-partition/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [find-the-value-of-the-partition](https://leetcode.cn/problems/find-the-value-of-the-partition/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,3,2,4]', '[100,1,10]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        #对有序的nums遍历其相邻的两个元素，找最小差\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        min_val = inf\n",
    "        for i in range(n-1):\n",
    "            if nums[i+1]-nums[i]<min_val:\n",
    "                min_val = nums[i+1]-nums[i]\n",
    "        return min_val\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:  \n",
    "\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:  \n",
    "\n",
    "        nums = sorted(nums)  \n",
    "\n",
    "        min_diff = float('inf')  \n",
    "\n",
    "        n = len(nums)  \n",
    "\n",
    "          \n",
    "\n",
    "        for i in range(1, n):  \n",
    "\n",
    "            diff = abs(nums[i] - nums[i-1])  \n",
    "\n",
    "            min_diff = min(min_diff, diff)  \n",
    "\n",
    "          \n",
    "\n",
    "        return min_diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        #对有序的nums遍历其相邻的两个元素，找最小差\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        min_val = inf\n",
    "        for i in range(n-1):\n",
    "            if nums[i+1]-nums[i]<min_val:\n",
    "                min_val = nums[i+1]-nums[i]\n",
    "        return min_val\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return min(abs(a - b) for a, b in pairwise(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums2 = sorted(nums)\n",
    "        num = float(\"inf\")\n",
    "        for i in range(len(nums)-1):\n",
    "            num = min(num, nums2[i+1]-nums2[i])\n",
    "        \n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        \n",
    "        i = 0\n",
    "        j = 1\n",
    "        n = len(nums)\n",
    "        ret = sys.maxsize\n",
    "        for _ in range(0, n-1):\n",
    "            ret = min(abs(nums[i] - nums[j]), ret)\n",
    "            i += 1\n",
    "            j += 1\n",
    "        return ret\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        ans=inf\n",
    "        nums.sort()\n",
    "        for i in range(1,len(nums)):\n",
    "            ans=min(ans,nums[i]-nums[i-1])\n",
    "        \n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        min=100000000000000000000000\n",
    "        for i in range(0,len(nums)-1):\n",
    "            if nums[i+1]-nums[i]<min:\n",
    "                min=nums[i+1]-nums[i]\n",
    "\n",
    "        return min\n",
    "            \n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        return min(y-x for x,y in pairwise(sorted(nums)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res = math.inf\n",
    "        for i in range(1,len(nums)):\n",
    "            res = min(res,nums[i]-nums[i-1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        size=len(nums)\n",
    "        ans=10**9\n",
    "        for i in range(1,size):\n",
    "            ans=min(ans,(nums[i]-nums[i-1]))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        return min(a[1] - a[0] for a in pairwise(sorted(nums)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        pr=nums[0]\n",
    "        ans =inf\n",
    "        for x in nums[1:]:\n",
    "          ans=min(ans,x-pr)\n",
    "          pr=x\n",
    "        return ans if len(nums)>=3 else abs(nums[-1]-nums[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res = math.inf\n",
    "        for i in range(1,len(nums)):\n",
    "            res = min(res,nums[i]-nums[i-1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        ans = 10**9\n",
    "        for i in range(1, len(nums)):\n",
    "            ans = min(ans, nums[i] - nums[i - 1])\n",
    "            if ans == 0:\n",
    "                return 0\n",
    "\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        ans = 9999999999\n",
    "        for i in range(n-1):\n",
    "            ans = min(nums[i+1]-nums[i],ans)\n",
    "\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return min(y - x for x,y in pairwise(nums))\n",
    "        \n",
    "        # nums.sort()\n",
    "        # n = len(nums)\n",
    "        # ans = inf\n",
    "        # for i in range(1,n):\n",
    "        #     ans = min(ans, nums[i] - nums[i-1])\n",
    "        # return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        n=len(nums)\n",
    "        ans=inf\n",
    "        for i in range(n-1):\n",
    "            ans=min(ans,abs(nums[i]-nums[i+1]))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res = math.inf\n",
    "        for i in range(1,len(nums)):\n",
    "            res = min(res,nums[i]-nums[i-1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        ans = float('inf')\n",
    "        for i in range(n-1):\n",
    "            ans = min(ans, abs(nums[i]-nums[i+1]))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        ans = 10 ** 9\n",
    "        nums.sort()\n",
    "        for i in range(1, len(nums)):\n",
    "            ans = min(ans, nums[i] - nums[i - 1])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        res = abs(nums[0]-nums[1])\n",
    "        for i in range(2,len(nums)):\n",
    "            new_res = abs(nums[i]-nums[i-1])\n",
    "            if new_res <res:\n",
    "                res = new_res\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res = math.inf\n",
    "        for i in range(1,len(nums)):\n",
    "            res = min(res,nums[i]-nums[i-1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return min(y-x for x,y in pairwise(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res = math.inf\n",
    "        for i in range(1,len(nums)):\n",
    "            res = min(res,nums[i]-nums[i-1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        temp = nums[1] - nums[0]\n",
    "        nums1 = [nums[0]]\n",
    "        nums2 = [nums[1]]\n",
    "        for i in range(2, len(nums)):\n",
    "            if nums[i] - nums[i - 1] < temp:\n",
    "                nums1.append(nums2.pop())\n",
    "                temp = nums[i] - nums[i - 1]\n",
    "            nums2.append(nums[i])\n",
    "        # print(nums1, nums2)\n",
    "        return temp\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res = 10**9\n",
    "        for i in range(1,len(nums)):\n",
    "            res = min(res,nums[i]-nums[i-1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        res = 9999999999999\n",
    "        for i in range(1, len(nums)):\n",
    "            res = min(res, nums[i] - nums[i - 1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return min(y - x for x, y in pairwise(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        count = 10 **9\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        for i in range(1,n):\n",
    "            res = nums[i] - nums[i-1]\n",
    "            if res < count:\n",
    "                count = res\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "\n",
    "        res=inf\n",
    "\n",
    "        nums.sort()\n",
    "\n",
    "        for i,x in enumerate(nums[:-1]):\n",
    "            res=min(res,abs(x-nums[i+1]))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "      nums.sort()\n",
    "      ans = 1000000000\n",
    "      for i in range(len(nums) - 1):\n",
    "        ans = min(ans, abs(nums[i+1] - nums[i]))\n",
    "      return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return min(y - x for x, y in pairwise(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return min(y - x for x, y in pairwise(nums))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        n=len(nums)\n",
    "        ans=inf \n",
    "        for i in range(n-1):\n",
    "            ans=min(ans,nums[i+1]-nums[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        return min(b-a for a,b in pairwise(sorted(nums)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return min(nums[i] - nums[i-1] for i in range(1, len(nums)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        ans=inf\n",
    "        for i in range(len(nums)-1):\n",
    "            ans=min(ans,nums[i+1]-nums[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res = nums[1] - nums[0]\n",
    "        for i in range(2, len(nums)):\n",
    "            res = min(res, nums[i] - nums[i-1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        min_diff= 0x3f3f3f3f\n",
    "        for a , b in pairwise(nums) :\n",
    "            min_diff = min(min_diff,b-a)\n",
    "        return min_diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from itertools import pairwise\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return min(j - i for i, j in pairwise(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return min(y - x for x, y in pairwise(nums))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        dp = [0] * (n - 1)\n",
    "        for i in range(1,n):\n",
    "            dp[i - 1] = nums[i] - nums[i - 1]\n",
    "        dp.sort()\n",
    "        return dp[0]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        n=len(nums)\n",
    "        ans=float(\"inf\")\n",
    "        for i in range(n-1):\n",
    "            cur=nums[i+1]-nums[i]\n",
    "            if cur<ans:\n",
    "                ans=cur\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        res = inf\n",
    "        for i in range(n - 1):\n",
    "            res = min(res, nums[i+1]-nums[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        ans = 9999999999\n",
    "        for i in range(len(nums)-1):\n",
    "            t = nums[i+1]-nums[i]\n",
    "            if t<ans:\n",
    "                ans = t\n",
    "\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return min(y - x for x, y in pairwise(nums))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        \n",
    "        ans = (1<<31) - 1\n",
    "        for i in range(1, len(nums)):\n",
    "            ans = min(ans, nums[i] - nums[i-1])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return min(nums[i+1]-nums[i] for i in range(len(nums)-1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return min([nums[i] - nums[i-1] for i in range(1, len(nums))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        res=inf\n",
    "        nums.sort()\n",
    "        for i,x in enumerate(nums[:-1]):\n",
    "            res=min(res,nums[i+1]-x)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        分区值 = float('inf')\n",
    "        for i in range(1, len(nums)):\n",
    "            分区值 = min(分区值, nums[i]-nums[i-1])\n",
    "        return  分区值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        min1=10**9\n",
    "        for i in range(1,len(nums)):\n",
    "            min1=min(min1,nums[i]-nums[i-1])\n",
    "        return min1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findValueOfPartition(self, nums: List[int]) -> int:\n",
    "        return min([a2-a1 for a1, a2 in pairwise(sorted(nums))])"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
